﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using PetaPoco;

namespace DAL.MySQL
{
    public class infoDAL
    {
        public Database db = DbScoure.DataBase();
        public infoEntity GetOne(int id)
        {
            return db.SingleOrDefault<infoEntity>("select * from info where id=@0", id);
        }

        public infoEntity GetOneByWebUrl(string weburl)
        {
            return db.SingleOrDefault<infoEntity>("select * from info where weburl=@0", weburl);
        }

        public List<infoEntity> GetList(string where, string order, params object[] args)
        {
            var list = db.Fetch<infoEntity>("select * from info where 1=1 " + where + " order by " + order + " ,ID asc", args);
            return list;
        }

        public Page<infoEntity> GetPageList(int pagenum, int pagesize, string where, string order, params object[] args)
        {
            var pagelist = db.Page<infoEntity>(pagenum, pagesize, "select * from info where 1=1 " + where + " order by " + order + " ,ID asc", args);
            return pagelist;
        }
        public int Update(object obj, int ID)
        {
            return db.Update("info", "ID", obj, ID);
        }
        public int UpdateHits(int ID)
        {
            return db.Execute("update info set hits=hits+1 where ID=@0", ID);
        }
        public infoEntity Save(infoEntity model)
        {
            db.Save("info", "ID", model);
            return model;
        }
        public bool Del(int id)
        {
            try
            {
                db.Delete("info", "ID", null, id);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool Del(infoEntity model)
        {
            try
            {
                db.Delete("info", "ID", model);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
